<?php

require_once 'PipesImpl/Config.php';
require_once 'PipesImpl/Route.php';

class Pipes_Route implements PipesImpl_Route
{
    private $_data   = array();

    public function __construct(PipesImpl_Config $config, $route) {
        $route = str_replace($config->base_uri, '', $route);
        
        preg_match('/\/?([a-z0-9_-]+)(\/(([a-z0-9_-]+)\/?)?(.*))?/i', $route, $matches);
        
        $this->_data['controller'] = $matches[1];
        if (!empty($matches[4])) {
            $this->_data['action'] = $matches[4];
        }

        if (!empty($matches[5])) {
            $extras = explode('?', $matches[5]);
            $this->_data['extras'] = explode('/', array_shift($extras));
            if (empty($this->_data['extras'][0])) {
                $this->_data['extras'] = array();
            }
            parse_str(array_shift($extras), $queryString);
            $this->_data['extras'] = array_merge($this->_data['extras'], $queryString);
        }
    }

    public function __get($key) {
        switch($key) {
            case 'controller' :
            case 'action' :
            case 'extras' :
                return $this->_data[$key];
                break;
        }
    }

    public function __isset($key) {
        return isset($this->_data[$key]);
    }
}
